﻿using Abp.Dependency;

using Abp.Events.Bus.Entities;
using Abp.Events.Bus.Handlers;
using Abp.RemoteEventBus;
using Abp.Runtime.Caching;
using FwProject.AppHome.Dto;
using FwProject.Comm;
using FwProject.Shop.Dto;
using FwProject.ShoppingMall.Shop;
using System;
using System.Collections.Generic;
using System.Text;

namespace FwProject.EventHandle
{
    public class ShopProductHandle : IEventHandler<EntityCreatedEventData<ShopProduct>>,IEventHandler<EntityUpdatedEventData<ShopProduct>>,
        IEventHandler<EntityDeletingEventData<ShopProduct>>,
        ITransientDependency
    {
        public readonly ICacheManager _catchmanager;
        public IRemoteEventBus remoteEventBus { get; set; }
      
        public ShopProductHandle(ICacheManager catchmanager)
        {
            remoteEventBus = NullRemoteEventBus.Instance;
            _catchmanager = catchmanager;
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="eventData"></param>
        public void HandleEvent(EntityCreatedEventData<ShopProduct> eventData)
        {
            _catchmanager.GetCache(nameof(HomeDto)).Remove("index");
            ///生成商品基本信息
            var catcheventDate = new RemoteEventData(RemoteTypeHelp.TYPESEARCH)
            {
                Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Id, [RemoteTypeHelp.MODELNAME] = nameof(ShopProductDto) }
            };
            remoteEventBus.Publish(RemoteTypeHelp.TOPICAPPSYSTEMSEARCH, catcheventDate);
            ///生成具体的明细情况
            var shopappcatcheventDate = new RemoteEventData(RemoteTypeHelp.TYPESEARCH)
            {
                Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Id, [RemoteTypeHelp.MODELNAME] = nameof(ShopProductAppDto) }
            };
            remoteEventBus.Publish(RemoteTypeHelp.TOPICAPPSYSTEMSEARCH, shopappcatcheventDate);

            ///生成列表的基本信息
            var shopproductListAppDto = new RemoteEventData(RemoteTypeHelp.TYPESEARCH)
            {
                Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Id, [RemoteTypeHelp.MODELNAME] = nameof(ShopProductListAppDto) }
            };
            remoteEventBus.Publish(RemoteTypeHelp.TOPICAPPSYSTEMSEARCH, shopproductListAppDto);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="eventData"></param>
        public void HandleEvent(EntityUpdatedEventData<ShopProduct> eventData)
        {
            _catchmanager.GetCache(nameof(HomeDto)).Remove("index");
            ///生成商品基本信息
            var catcheventDate = new RemoteEventData(RemoteTypeHelp.TYPESEARCH)
            {
                Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Id, [RemoteTypeHelp.MODELNAME] = nameof(ShopProductDto) }
            };
            remoteEventBus.Publish(RemoteTypeHelp.TOPICAPPSYSTEMSEARCH, catcheventDate);
            ///生成具体的明细情况
            var shopappcatcheventDate = new RemoteEventData(RemoteTypeHelp.TYPESEARCH)
            {
                Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Id, [RemoteTypeHelp.MODELNAME] = nameof(ShopProductAppDto) }
            };
            remoteEventBus.Publish(RemoteTypeHelp.TOPICAPPSYSTEMSEARCH, shopappcatcheventDate);

            ///生成列表的基本信息
            var shopproductListAppDto = new RemoteEventData(RemoteTypeHelp.TYPESEARCH)
            {
                Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Id, [RemoteTypeHelp.MODELNAME] = nameof(ShopProductListAppDto) }
            };
            remoteEventBus.Publish(RemoteTypeHelp.TOPICAPPSYSTEMSEARCH, shopproductListAppDto);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="eventData"></param>
        public void HandleEvent(EntityDeletingEventData<ShopProduct> eventData)
        {
            _catchmanager.GetCache(nameof(HomeDto)).Remove("index");
            ///生成商品基本信息
            var catcheventDate = new RemoteEventData(RemoteTypeHelp.TYPEDELETESEARCH)
            {
                Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Id, [RemoteTypeHelp.MODELNAME] = nameof(ShopProductDto) }
            };
            remoteEventBus.Publish(RemoteTypeHelp.TOPICAPPSYSTEMSEARCH, catcheventDate);
            ///生成具体的明细情况
            var shopappcatcheventDate = new RemoteEventData(RemoteTypeHelp.TYPEDELETESEARCH)
            {
                Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Id, [RemoteTypeHelp.MODELNAME] = nameof(ShopProductAppDto) }
            };
            remoteEventBus.Publish(RemoteTypeHelp.TOPICAPPSYSTEMSEARCH, shopappcatcheventDate);

            ///生成列表的基本信息
            var shopproductListAppDto = new RemoteEventData(RemoteTypeHelp.TYPEDELETESEARCH)
            {
                Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Id, [RemoteTypeHelp.MODELNAME] = nameof(ShopProductListAppDto) }
            };
            remoteEventBus.Publish(RemoteTypeHelp.TOPICAPPSYSTEMSEARCH, shopproductListAppDto);
        }
    }
}
